<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script type="text/javascript" src="./js/jquery-1.8.3.min.js"></script>
<script type="text/javascript" src="./js/angular.min.js"></script>
<style type="text/css">
.red{color:red;}
[ng-cloak]{display:none;}
[ng-cloak],.ng-cloak{display:none;}
</style>
</head>
<body>
<div ng-app="myApp" ng-controller="myAppController">
{{username}}
<p ng-repeat="x in userList">{{x.id}}----{{x.title}}----{{x.desc}}</p>
<hr>
{{usernameTwo}}
<p ng-repeat="xx in userListTwo">{{xx.id}}----{{xx.title}}----{{xx.desc}}</p>
</div>
<script type="text/javascript">
var url = "./PHP/1.post.php";
// 通过 provide 可以自定义服务
var myApp = angular.module("myApp",[],function($provide){	
	// service 服务
	$provide.service("customFour",function(){
		var name = "方法一";
		return {
			name : name,
			second : "方法二"
		};
	});
});
myApp.controller("myAppController",function($scope,$http,customFour){
	$scope.username = customFour.name;
	$scope.usernameTwo = customFour.second;
	$scope.userList = null;
	$scope.userListTwo = null;
	$scope.userList = [
		{id:1,title:'标题11111',desc:'描述11111'},
		{id:2,title:'标题22222',desc:'描述22222'}
	];	
	// 使用 $.post 来进行 post 利用 angular 的脏检查实现数据绑定
	$.post(url,{size:'5'},function(res){
		$scope.$apply(function(){
			$scope.userList = res;
		});
	},'json');
	// 使用angualr自带的 post 方法进行 post 请求
	// 多个请求考虑封装 angualr的post请求自带 脏检查
	ajaxPost(url,{name:'张三',size:6},function(res){
		$scope.userListTwo = res;
	});
	function ajaxPost(url,data,fun){
		$http({  
		    method:'post',  
		    url:url,  
		    data:data,  
		    headers:{'Content-Type': 'application/x-www-form-urlencoded'},  
		    transformRequest: function(obj) {  
		        var str = [];  
		        for(var p in obj){  
		            str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));  
		        }  
		        return str.join("&");  
		    }  
		}).success(function(res){
			fun(res);
		});
	};
});
</script>
</body>
</html>